<extend name="Public:template" />
<block name="page_header">
	<ul class="header-sub">
		<li class="previous"><a href="{:U('Index/index')}" class="btn btn-default active" role="button"><span class="icon icon-home"></span>首页</a></li>
		<li ><h3>配送地址管理</h3></li>
		<li class="next "><a href="{:U('Cust/index')}" class="btn btn-warning active" role="button">我的<span class="icon-white icon-user"></span></a></li>
	</ul>
</block>
<block name="page_body">
<div class="list-group mt15">
<volist name="list" id="val">
	<div class="list-group-item <if condition="$val['is_default'] eq 1">active</if>">
	<h4 class="list-group-item-heading"><img class="img-thumbnail" src="__ROOT__/statics/mobile/images/icons/house.png" style="margin-right:10px"><span id="address{$val.id}">{$val.address}</span><span class="icon icon-chevron-down pull-right"  style="position:absolute;top:20px;right:5px;"></span></h4>
	<form action="{:U('CustAddress/doUpd')}" class="list-group-item-text" style="display:none" method="post" id="form_upd{$val.id}" name="form_upd{$val.id}" role="form">
		<input type="hidden" name="id" value="{$val.id}">
		<div class="input-group mt5">
			<span class="input-group-addon alert-warning"><span class="icon icon-user"></span></span>
			<input type="text" class="form-control" name="name" placeholder="请输入收货人姓名" value="{$val.name}">
		</div>
		<div class="input-group  mt5">
			<span class="input-group-addon alert-warning"><span class="icon icon-tel"></span></span>
			<input type="tel" class="form-control" name="mobile" placeholder="请输入收货人手机或电话" value="{$val.mobile}">
		</div>
		<div class="input-group  mt5">
			<span class="input-group-addon alert-warning"><span class="icon icon-home"></span></span>
			<input type="text" class="form-control" name="address" placeholder="请输入您的收货地址" value="{$val.address}">
		</div>
		<div class="checkbox mt15">
			<label>
				<input type="checkbox"  name="is_default" value="1" <if condition="$val['is_default'] eq 1">checked</if>>常用地址
			</label>
		</div>
		<div class="row">
			<div class="col-xs-3 col-sm-3">
				<button type="button" class="btn btn-default active mt5" onClick="doDel('{$val.id}')"><span class="icon icon-trash"></span>删除</button>
			</div>
			<div class="col-xs-9 col-sm-9">
				<button type="button" class="btn alert-warning btn-block mt5" onClick="doUpd('{$val.id}')">更新</button>
			</div>
		</div>
		
	</form>
	</div>
</volist>
	<div class="list-group-item" id="address">
		<h4 class="list-group-item-heading"><img class="img-thumbnail" src="__ROOT__/statics/mobile/images/icons/add_green.png" style="margin-right:10px">添加新地址<span class="icon icon-chevron-down pull-right" style="position:absolute;top:20px;right:5px;"></span></h4>
		<form action="{:U('CustAddress/doAdd')}" class="list-group-item-text" style="display:none" method="post" id="form_add" name="form_add" role="form">
			<input type="hidden" name="lastUrl" value="{$lastUrl}">
			<div class="input-group mt5">
				<span class="input-group-addon alert-success"><span class="icon icon-user"></span></span>
				<input type="text" class="form-control" name="name" placeholder="请输入收货人姓名" value="{$cust.name}">
			</div>
			<div class="input-group  mt5">
				<span class="input-group-addon alert-success"><span class="icon icon-tel"></span></span>
				<input type="tel" class="form-control" name="mobile" placeholder="请输入收货人手机或电话" value="{$cust.mobile}">
			</div>
			<div class="input-group  mt5">
				<span class="input-group-addon alert-success"><span class="icon icon-home"></span></span>
				<input type="text" class="form-control" name="address" placeholder="请输入您的收货地址" value="">
			</div>
			<div class="checkbox mt15">
				<label>
					<input type="checkbox"  name="is_default" value="1" <empty name='list'>checked</empty>>常用地址
				</label>
			</div>
			<div class="row">
			<div class="col-xs-3 col-sm-3">
				<button type="button" class="btn btn-default active mt5"  onClick="doEmpty()"><span class="icon icon-refresh"></span>重置</button>
			</div>
			<div class="col-xs-9 col-sm-9">
				<button type="button" class="btn alert-success btn-block mt5" onClick="doAdd()">添加</button>
			</div>
		</div>
		</form>
	</div>
</div>
</block>
<block name="script_footer">
<script type="text/javascript">
$(document).ready(function(){
	$(".list-group-item").on('click',function(){
		$(".list-group-item").removeClass("active");
		$(".list-group-item").find(".list-group-item-text").hide();
		$(this).addClass("active");
		$(this).find(".list-group-item-text").show();
	})
})

function doEmpty(){
	document.forms['form_add'].name.value='';
	document.forms['form_add'].mobile.value='';
	document.forms['form_add'].address.value='';
}
function doUpd(id){
	var formId = "#form_upd"+id;
	$.ajax({
        'url': $(formId).attr("action"),
        'type':"post",
        'dataType': "json",
        'data': $(formId).serialize(),
        'success': function (data) {
        	var error = data['error'];
        	var message = data['message'];
        	if(error=='0'){
        		alert('更新成功！');
        		$("#address"+id).html($(formId).find("input[name='address']").val());
        	}else{
        		alert(message);
        	}
        	
        }
    });
	return false;
}

function doAdd(){
	var msgId = "#msg_info_add";
	var formId = "#form_add";
	$(msgId).hide();
	$.ajax({
        'url': $(formId).attr("action"),
        'type':"post",
        'dataType': "json",
        'data': $(formId).serialize(),
        'success': function (data) {
        	var error = data['error'];
        	var message = data['message'];
        	if(error==0){
        		alert('添加成功！');
        		document.location.href="/CustAddress/index.html";
        	}else{
        		alert(message);
        	}
        	
        }
    });
	return false;
}

function doDel(id){
	if(!confirm("确定删除该配送信息？")){
		return false;
	}
	var msgId = "#msg_info_upd"+id;
	var formId = "#form_upd"+id;
	$.ajax({
		'url': "{:U('CustAddress/doDel')}",
        'type':"post",
        'dataType': "json",
        'data': $(formId).serialize(),
        'success': function (data) {
        	var error = data['error'];
        	var message = data['message'];
        	if(error==0){
        		document.location.href="/CustAddress/index.html";
        	}else{
        		$(msgId).html(message);
            	$(msgId).show();
        	}
        	
        }
    });
	return false;
}
</script>
</block>